// 状态
import { getBaseCategoryList } from '@/api/product'
//定义状态数据state
const state = {
  // 三级分类数据
  categoryList: [],
  // 商品排行
  rankList: [],
  // 货物排行
  goodsList: [],
}
// action:异步行为
const actions = {
  // 获取三级数据分类 解构赋值
  async getBaseCategoryListAsync({ commit, state }, limit = 15) {
    //若categoryList.length > 0直接返回
    if (state.categoryList.length > 0) return
    const res = await getBaseCategoryList()
    commit('UP_CATEGORY_LIST', res.data.slice(0, limit))
  },
  //获取商品排行
  async getRankListAsync({ commit }) {
    const res = await getRankList()
    commit('UP_RANKLIST', res.rankList)
  },
  // 获取货物排行
  async getGoodsListAsync({ commit }, type = 1) {
    const res = await getGoodsList(type)
    commit('UP_GOODSLIST', res.goodsList)
  },
}
//定义mutations给数据状态
const mutations = {
  // 更新三级分类数据
  UP_CATEGORY_LIST(state, payload) {
    state.categoryList = payload
  },
  UP_RANKLIST(state, payload) {
    state.rankList = payload
  },
  UP_GOODSLIST(state, payload) {
    state.goodsList = payload
  },
}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
}
